<template>
	<view class="">
		<view class="" style="padding-bottom: 120rpx;">
			<view class="mainpadding ffffff">
				<view class="ershiba nofonweight xiaohei">背景图</view>
				<view class="margin_top shangc" v-if="!bg_image"  @click="uploadImg(1)">
					<image src="../static/image/system/shangchuan.png" mode=""></image>
				</view>
				<view class="margin_top shangc" v-if="bg_image" @click="uploadImg(1)">
					<image :src="bg_image_text" mode=""></image>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">详情描述图（{{des_images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in des_images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu"  @click="delindex(index,des_images_text,des_images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="des_images.length<5" @click="uploadImg(5,des_images,des_images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="mainpadding ffffff margin_top1">
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">权益卡名称</view>
					<input class="textright nofonweight" type="text" placeholder="输入权益卡名称" v-model="name">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">价格</view>
					<view class="flexright">
						<input class="textright nofonweight" type="number" placeholder="输入权益卡价格" v-model="price">
						<view class="ershiba nofonweight xiaohei">元</view>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">天数</view>
					<view class="flexright">
						<input class="textright nofonweight" type="number" placeholder="输入权益卡天数" v-model="days">
						<view class="ershiba nofonweight xiaohei">天</view>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">生效天数</view>
					<view class="flexright">
						<input class="textright nofonweight" type="number" placeholder="请输入" v-model="sx_days">
						<view class="ershiba nofonweight xiaohei">天后生效</view>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top" v-if="listid">
					<view class="ershiba nofonweight xiaohei">是否上架</view>
					<view class="flexright">
						<u-radio-group v-model="status" placement="row" circle>
							<u-radio activeColor="#1ba95b" :customStyle="{marginRight: '30rpx'}" label="启用"
								name="normal"></u-radio>
							<u-radio activeColor="#1ba95b" label="禁用" name="hidden"></u-radio>
						</u-radio-group>
					</view>
				</view>
			</view>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				name: "",
				listid:"",
				bg_image:"",
				bg_image_text:"",
				des_images:[],
				des_images_text:[],
				days:"",
				sx_days:"",
				price:"",
				status:"normal",//状态:normal=启用,hidden=禁用
				user_zbfapply_listid:""
			}
		},
		onLoad(options) {
			this.user_zbfapply_listid=options.user_zbfapply_listid
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "编辑权益卡"
				})
				this.listid = options.id
				this.getDetail()
			}
		},
		methods: {
			getDetail(){
				httpRequest.request('/api/user/userHyqycardDetail', 'GET', {
					id:this.listid,
					user_zbfapply_id:this.user_zbfapply_listid
				}).then(res => {
					let data = res.data
					this.name=data.name
					this.days=data.days
					this.price=data.price
					this.des_images=data.des_images.split(',')
					this.des_images_text=data.des_images_text
					this.bg_image=data.bg_image
					this.bg_image_text=data.bg_image_text
					this.status = data.status
					this.sx_days = data.sx_days
				})
			},
			submit(){
				if(this.bg_image==""){
					httpRequest.toast("请上传权益卡背景图")
					return false
				}
				if(this.des_images.length==0){
					httpRequest.toast("请上传详情图")
					return false
				}
				if(this.name==""){
					httpRequest.toast("请输入名称")
					return false
				}
				if(this.price==""){
					httpRequest.toast("请输入价格")
					return false
				}
				if(this.days==""){
					httpRequest.toast("请输入天数")
					return false
				}
				if(this.sx_days==""){
					httpRequest.toast("请输入生效天数")
					return false
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				let url = "/api/user/addUserHyqycard"
				let data = {
					name:this.name,
					days:this.days,
					sx_days:this.sx_days,
					price:this.price,
					des_images:this.des_images.join(),
					bg_image:this.bg_image,
					user_zbfapply_id:this.user_zbfapply_listid
				}
				if(this.listid){
					url = "/api/user/editUserHyqycard"
					this.$set(data,"id",this.listid)
					this.$set(data,"status",this.status)
				}
				httpRequest.request(url, 'POST', data).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			},
			
			// 跳转完成创建
			tzwccj() {
				uni.navigateTo({
					url: '/pages_julebu/chuangjianwc'
				})
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				this.upvalue = value
				let num = 0
				if(value==5){
					num = Number(value) - upImgs.length;
					if (num <= 0) {
						return false;
					}
				}else{
					num = 1
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					if(this.upvalue==1){
						this.bg_image = res.url
						this.bg_image_text = image_item
						return false
					}
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.touxiang {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;

		image {
			border-radius: 50%;
			width: 100%;
			height: 100%;
		}
	}

	.dizneo {
		height: 500rpx;
		overflow: auto;
	}

	.xiaolbtn {
		width: 112rpx;
		height: 48rpx;
		background: rgba(27, 169, 91, 0.1);
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #1BA95B;
		border: 2rpx solid #1BA95B;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {

		background: #F9F9F9;
	}

	.shanchu {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
	}

	.shanchu2 {
		position: absolute;
		bottom: -10rpx;
		right: -50%;
		transform: translate(0, -50%);
	}

	.shilitu {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shangc {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>